package cn.itsource.pethome.pet.controller;

import cn.itsource.pethome.basic.util.AjaxResult;
import cn.itsource.pethome.basic.util.PageList;
import cn.itsource.pethome.basic.util.SystemContext;
import cn.itsource.pethome.org.domain.Employee;
import cn.itsource.pethome.org.domain.Shop;
import cn.itsource.pethome.org.service.IShopService;
import cn.itsource.pethome.pet.domain.Pet;
import cn.itsource.pethome.pet.query.PetQuery;
import cn.itsource.pethome.pet.service.IPetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 宠物
 */
@RestController
@RequestMapping("/pet")
public class PetController {


    @Autowired
    private IShopService shopService;
    @Autowired
    private IPetService petService;



    /**
     * 查询所有的类别
     * @param petQuery
     * @return
     */
    @PatchMapping("/getAll")
    public PageList<Pet> getAll(@RequestBody PetQuery petQuery){
        //获取到登录用户
        Employee systemLoginUser = SystemContext.getSystemLoginUser();

        Shop shop = shopService.loadByEmployee(systemLoginUser);
        //根据登录用户查询店铺
        petQuery.setShopId(shop.getId());
        //分页查询
        return petService.loadPageByQuery(petQuery);
    }


    /**
     * 上架
     * @param pets
     * @return
     */
    @PatchMapping("/onSale")
    public AjaxResult onSale(@RequestBody List<Pet> pets){
        try {
            //上线
            petService.onSale(pets, 1);
            return new AjaxResult();
        }catch (Exception e){
            e.printStackTrace();
            return new AjaxResult(false, e.getMessage());
        }
    }
    /**
     * 下架
     * @param pets
     * @return
     */
    @PatchMapping("/offSale")
    public AjaxResult offSale(@RequestBody List<Pet> pets){
        try {
            //下架
            petService.offSale(pets, 0);
            return new AjaxResult();
        }catch (Exception e){
            e.printStackTrace();
            return new AjaxResult(false, e.getMessage());
        }
    }
//================================前台界面=======================================================

    /**
     * 前台查询所有的宠物
     * @param petQuery
     * @return
     */
    @PatchMapping("/loadAll")
    public PageList<Pet> loadAll(@RequestBody PetQuery petQuery){

        //根据登录用户查询店铺
        petQuery.setState(1);
        //分页查询
        return petService.loadPageByQuery(petQuery);
    }


    /**
     * 根据id查询宠物详细信息
     * @return
     */
    @GetMapping("{id}")
    public Pet loadPetById(@PathVariable("id") Long id) {

        return petService.loadById(id);
    }




}
